import {
  Controller,
  Get,
  Post,
  Put,
  Delete,
  Body,
  Param,
} from '@nestjs/common';
import { MenuService } from './menu.service';
import { CreateMenuDto } from './dto/create-menu.dto';
import { UpdateMenuDto } from './dto/update-menu.dto';
import { Menu } from './entities/menu.entity';
import {
  ApiTags,
  ApiOperation,
  ApiResponse,
  ApiParam,
  ApiBody,
} from '@nestjs/swagger';
import { Response } from '../../infra/schema/response';
import { Public } from 'src/auth/auth.decorator';
import transformMenuData from '../../infra/common/transformMenu';
@ApiTags('菜单管理')
@Controller('menu')
export class MenuController {
  constructor(private readonly menuService: MenuService) {}

  /**
   * 创建菜单
   * @param createMenuDto 菜单创建信息
   * @returns 创建好的菜单信息
   */
  @Post()
  @ApiOperation({ summary: '创建菜单' })
  @ApiBody({ type: CreateMenuDto })
  @ApiResponse({ status: 201, description: '菜单创建成功', type: Menu })
  create(@Body() createMenuDto: CreateMenuDto) {
    return this.menuService.create(createMenuDto);
  }

  /**
   * 获取所有菜单
   * @returns 所有菜单列表
   */
  @Public()
  @Get()
  @ApiOperation({ summary: '获取所有菜单' })
  @ApiResponse({ status: 200, description: '成功获取菜单列表', type: [Menu] })
  async findAll(): Promise<any> {
    const menuList = await this.menuService.findAll();
    // const transformedMenuList = transformMenuData(menuList);
    // console.log(transformedMenuList, '???????????');
    return Response.success(menuList);
  }

  /**
   * 根据 ID 获取单个菜单
   * @param id 菜单 ID
   * @returns 对应 ID 的菜单信息
   */
  @Get(':id')
  @ApiOperation({ summary: '根据 ID 获取单个菜单' })
  @ApiParam({ name: 'id', description: '菜单 ID', type: Number })
  @ApiResponse({ status: 200, description: '成功获取菜单信息', type: Menu })
  @ApiResponse({ status: 404, description: '菜单未找到' })
  findOne(@Param('id') id: string) {
    return this.menuService.findOne(+id);
  }

  /**
   * 根据 ID 更新菜单
   * @param id 菜单 ID
   * @param updateMenuDto 菜单更新信息
   * @returns 更新后的菜单信息
   */
  @Put(':id')
  @ApiOperation({ summary: '根据 ID 更新菜单' })
  @ApiParam({ name: 'id', description: '菜单 ID', type: Number })
  @ApiBody({ type: UpdateMenuDto })
  @ApiResponse({ status: 200, description: '菜单更新成功', type: Menu })
  @ApiResponse({ status: 404, description: '菜单未找到' })
  update(@Param('id') id: string, @Body() updateMenuDto: UpdateMenuDto) {
    return this.menuService.update(+id, updateMenuDto);
  }

  /**
   * 根据 ID 删除菜单
   * @param id 菜单 ID
   * @returns 删除操作结果
   */
  @Delete(':id')
  @ApiOperation({ summary: '根据 ID 删除菜单' })
  @ApiParam({ name: 'id', description: '菜单 ID', type: Number })
  @ApiResponse({ status: 200, description: '菜单删除成功' })
  @ApiResponse({ status: 404, description: '菜单未找到' })
  remove(@Param('id') id: string) {
    return this.menuService.remove(+id);
  }
}
